package com.charity.service.serviceImpl;

import com.charity.mapper.DonationMapper;
import com.charity.model.bo.*;
import com.charity.pojo.Donation;
import com.charity.service.CharityService;
import com.charity.service.DonationService;
import com.charity.service.EntryService;
import com.charity.service.ServiceManager;
import org.fisco.bcos.sdk.transaction.model.dto.CallResponse;
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DonationServiceImpl implements DonationService {

    @Autowired
    private DonationMapper donationMapper;

    @Autowired
    private CharityService charityService;

    @Autowired
    private EntryService entryService;

    @Autowired
    private ServiceManager serviceManager;

    @Override
    public List<Donation> bangDan(Integer month) {
        return donationMapper.bangDan(month);
    }

    @Override
    public Integer addDonation(Long id, Long donorId, String donorName, Long donationAmount, Long entryId, String createDate, String state) throws Exception {
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharityAddDonationInputBO addDonationInputBO = new CharityAddDonationInputBO(id, donorId, donorName, donationAmount, entryId, new Date().getTime());
        TransactionResponse addDonation = charityService.addDonation(addDonationInputBO);
        Integer result = donationMapper.addDonation(id, donorId, donorName, donationAmount, entryId, createDate, state, "未发放");
        if (result>0) {
            return result;
        }
        return 0;
    }

    @Override
    public Donation selById(Long id) throws Exception {
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharitySearchDonationByidInputBO searchDonationByidInputBO = new CharitySearchDonationByidInputBO(id);
        charityService.searchDonationByid(searchDonationByidInputBO);
        return donationMapper.selById(id);
    }

    @Override
    public List<Donation> selByDonorId(Long donorId) throws Exception {
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharitySearchDonationBydonoridInputBO searchDonationBydonoridInputBO = new CharitySearchDonationBydonoridInputBO(donorId);
        charityService.searchDonationBydonorid(searchDonationBydonoridInputBO);
        return donationMapper.selByDonorId(donorId);
    }

    @Override
    public List<Donation> selBySH() {
        return donationMapper.selBySH();
    }

    @Override
    public Integer updSh(Donation donation) throws Exception {
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharityUpdateDonationInputBO updateDonationInputBO = new CharityUpdateDonationInputBO(donation.getId(), donation.getState(), donation.getReviewer(), "");
//        charityService.updateDonation(updateDonationInputBO);
        CharityUpdateEntryTocolInputBO updateEntryTocolInputBO = new CharityUpdateEntryTocolInputBO(donation.getId(), donation.getDonationAmount());
//        charityService.updateEntryTocol(updateEntryTocolInputBO);
        entryService.updEntryCol(donation.getDonationAmount(), donation.getEntryId());
        return donationMapper.updSh(donation.getId(), "通过", donation.getReviewer());
    }

    @Override
    public List<Donation> selWTG() {
        return donationMapper.selWTG();
    }

    @Override
    public Integer delD(Long id) throws Exception {
        serviceManager.init();
        Map<String, CharityService> map = serviceManager.initCharityServiceManager();
        List<String> k = new ArrayList<>();
        for (String key : map.keySet()) {
            k.add(key);
        }
        charityService = map.get(k.get(0));
        charityService.init();
        CharityDeleteDonationInputBO deleteDonationInputBO = new CharityDeleteDonationInputBO(id);
        charityService.deleteDonation(deleteDonationInputBO);
        return donationMapper.delD(id);
    }

    @Override
    public Integer updBC(Long entryId, String account) {
        return donationMapper.updBC(entryId, account);
    }

    @Override
    public Integer upFF(Long id, String account) {
        return donationMapper.upFF(id, account);
    }
}
